﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CreateNewReportCategorySelectorViewModel.cs" company="Spenta Consulting">
//   (c) Copyright 2011
// </copyright>
// <summary>
//   Category selector view model for create new report
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Spenta.StreetCare.Silverlight.MVVM.ViewModels
{
    #region Using Directives

    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Browser;
    using System.Windows.Input;
    using GalaSoft.MvvmLight.Command;
    using GalaSoft.MvvmLight.Messaging;

    using Spenta.StreetCare.Silverlight.MVVM.Messages;

    using StreetCareAPI;

    #endregion

    /// <summary>
    /// Category selector view model for create new report
    /// </summary>
    public class CreateNewReportCategorySelectorViewModel : BaseViewModel
    {
        /// <summary>
        /// Private store for categories
        /// </summary>
        private IEnumerable<CategoryWithCount> categories;

        #region Class Constructor

        /// <summary>
        /// Initializes a new instance of the <see cref="CreateNewReportCategorySelectorViewModel"/> class.
        /// </summary>
        public CreateNewReportCategorySelectorViewModel()
        {
            if (HtmlPage.IsEnabled)
            {
                Messenger.Default.Register<IEnumerable<CategoryWithCount>>(this, c => this.Categories = c);
                Messenger.Default.Register<NewReportMessage>(this, this.InitReportCategory);
            }
        }

        #endregion

        #region ViewModel Properties

        /// <summary>
        /// Gets or sets the selectable categories.
        /// </summary>
        /// <value>The selectable categories.</value>
        public IEnumerable<CategoryWithCount> Categories
        {
            get
            {
                return this.categories;
            }

            set 
            { 
                this.categories = value;
                RaisePropertyChanged("Categories");
            }
        }

        #endregion

        #region Commands

        /// <summary>
        /// Gets the category selected command.
        /// </summary>
        /// <value>The category selected command.</value>
        public ICommand CategorySelectedCommand
        {
            get
            {
                return new RelayCommand<Category>(CategorySelected);
            }
        }

        /// <summary>
        /// Handles the category selected command.
        /// </summary>
        /// <param name="category">The category.</param>
        private static void CategorySelected(Category category)
        {
            Messenger.Default.Send(category);
        }

        /// <summary>
        /// Inits the report category.
        /// </summary>
        /// <param name="message">The message.</param>
        private void InitReportCategory(NewReportMessage message)
        {
            if (this.Categories != null)
            {
                var category = this.Categories.FirstOrDefault();
                if (category != null)
                {
                    category.IsDefault = true;
                    this.RaisePropertyChanged("Categories");
                    Messenger.Default.Send(category as Category);
                }
            }
        }

        #endregion
    }
}
